﻿Imports System.Data
Imports System.Data.SqlClient
Imports CondorTravel.BE
Imports CondorTravelUTIL.DL


Public Class UsuariosDALC


    Public Function ListarUsuarios() As List(Of UsuariosBE)
        Dim sqlListar As String
        Dim objListaUsuarios As List(Of UsuariosBE)
        Dim objListaUsuariosBE As UsuariosBE = Nothing
        Dim ds As DataSet
        Dim dt As DataTable

        Try

            'sqlListar = "SP_UsuarioList_SolServ"
            sqlListar = "SP_UsuarioList"

            Dim objDatos As New AccesoDatos()
            Dim blnExito As Boolean
            Dim strMensaje As String = String.Empty
            Dim intI As Integer = 0

            ds = objDatos.DevuelveDataSet(sqlListar, blnExito, strMensaje, Nothing)
            dt = ds.Tables(0)


            objListaUsuarios = New List(Of UsuariosBE)()

            If dt.Rows.Count > 0 Then

                While intI < dt.Rows.Count
                    objListaUsuariosBE = New UsuariosBE()
                    objListaUsuariosBE.CodUsuario = dt.Rows(intI).ItemArray(0).ToString
                    objListaUsuariosBE.NomUsuario = dt.Rows(intI).ItemArray(1).ToString
                    intI = intI + 1

                    objListaUsuarios.Add(objListaUsuariosBE)
                End While
            End If

        Catch ex As Exception
            MsgBox(ex.Message, vbCritical, "Error del sistema")
            Return Nothing
            Exit Function
        End Try

        Return objListaUsuarios
    End Function

    Public Function ValidarLogin(ByVal strUsuario As String, ByVal strPassword As String, ByRef bExito As Boolean, ByRef sMensaje As String, ByRef sCodigoRol As String) As Boolean
        Dim arrSqlParameter(3) As SqlParameter
        Dim sqlValidaLogin As String
        Dim iFlagLogin As Integer

        Try
            '1. Definir la sentencia a ejecutar
            sqlValidaLogin = "SP_ValidaLogin"

            '2. Definir e instanciar los parámetros a utilizar

            arrSqlParameter(0) = New SqlParameter()
            arrSqlParameter(0).ParameterName = "@C_COD_USUARIO"
            arrSqlParameter(0).SqlDbType = SqlDbType.VarChar
            arrSqlParameter(0).Size = 10
            arrSqlParameter(0).Direction = ParameterDirection.Input
            arrSqlParameter(0).Value = strUsuario

            arrSqlParameter(1) = New SqlParameter()
            arrSqlParameter(1).ParameterName = "@T_PASS_USUARIO"
            arrSqlParameter(1).SqlDbType = SqlDbType.VarChar
            arrSqlParameter(1).Size = 10
            arrSqlParameter(1).Direction = ParameterDirection.Input
            arrSqlParameter(1).Value = strPassword

            arrSqlParameter(2) = New SqlParameter()
            arrSqlParameter(2).ParameterName = "@N_FLAG_LOGIN"
            arrSqlParameter(2).SqlDbType = SqlDbType.Int
            arrSqlParameter(2).Direction = ParameterDirection.Output
            arrSqlParameter(2).Value = 0

            arrSqlParameter(3) = New SqlParameter()
            arrSqlParameter(3).ParameterName = "@C_COD_ROL"
            arrSqlParameter(3).SqlDbType = SqlDbType.VarChar
            arrSqlParameter(3).Size = 10
            arrSqlParameter(3).Direction = ParameterDirection.Output
            arrSqlParameter(3).Value = ""


            Dim oDatos As New AccesoDatos()

            oDatos.EjecutaSP(sqlValidaLogin, bExito, sMensaje, arrSqlParameter)

            If bExito Then
                iFlagLogin = oDatos.Parametro("@N_FLAG_LOGIN")
                sCodigoRol = oDatos.Parametro("@C_COD_ROL")
                If iFlagLogin = 1 Then
                    Return True
                Else
                    sMensaje = "Su usuario y/o password es incorrecto"
                End If

            End If

            Return False

        Catch ex As Exception
            Throw
        End Try
    End Function


End Class
